<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>商城共通方法 | WUU-UI</title>
    <meta name="generator" content="VuePress 1.8.2">
    
    <meta name="description" content="WUU-UI">
    
    <link rel="preload" href="/mall-ui/assets/css/0.styles.fb3964a0.css" as="style"><link rel="preload" href="/mall-ui/assets/js/app.71961e7f.js" as="script"><link rel="preload" href="/mall-ui/assets/js/2.f1cb9681.js" as="script"><link rel="preload" href="/mall-ui/assets/js/13.06b07c8e.js" as="script"><link rel="prefetch" href="/mall-ui/assets/js/10.11912e0a.js"><link rel="prefetch" href="/mall-ui/assets/js/11.bca6f591.js"><link rel="prefetch" href="/mall-ui/assets/js/12.b3358f86.js"><link rel="prefetch" href="/mall-ui/assets/js/3.4e208c7e.js"><link rel="prefetch" href="/mall-ui/assets/js/4.110a989f.js"><link rel="prefetch" href="/mall-ui/assets/js/5.c89a64a3.js"><link rel="prefetch" href="/mall-ui/assets/js/6.83fdb325.js"><link rel="prefetch" href="/mall-ui/assets/js/7.be3d3bc1.js"><link rel="prefetch" href="/mall-ui/assets/js/8.48f4c41d.js"><link rel="prefetch" href="/mall-ui/assets/js/9.1c35ec6b.js">
    <link rel="stylesheet" href="/mall-ui/assets/css/0.styles.fb3964a0.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/mall-ui/" class="home-link router-link-active"><img src="/mall-ui/images/wuu.png" alt="WUU-UI" class="logo"> <span class="site-name can-hide">WUU-UI</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/mall-ui/comps/configure/introduce.html" class="nav-link">
  指南
</a></div><div class="nav-item"><a href="/mall-ui/comps/assembly/slider.html" class="nav-link">
  组件
</a></div><div class="nav-item"><a href="/mall-ui/comps/readme/wxsc.html" aria-current="page" class="nav-link router-link-exact-active router-link-active">
  JS
</a></div><div class="nav-item"><a href="/mall-ui/comps/about/index.html" class="nav-link">
  关于我们
</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/mall-ui/comps/configure/introduce.html" class="nav-link">
  指南
</a></div><div class="nav-item"><a href="/mall-ui/comps/assembly/slider.html" class="nav-link">
  组件
</a></div><div class="nav-item"><a href="/mall-ui/comps/readme/wxsc.html" aria-current="page" class="nav-link router-link-exact-active router-link-active">
  JS
</a></div><div class="nav-item"><a href="/mall-ui/comps/about/index.html" class="nav-link">
  关于我们
</a></div> <!----></nav>  <ul class="sidebar-links"><li><a href="/mall-ui/comps/readme/wxsc.html" aria-current="page" class="active sidebar-link">商城共通方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/mall-ui/comps/readme/wxsc.html#_1-数组去重" class="sidebar-link">1. 数组去重</a></li><li class="sidebar-sub-header"><a href="/mall-ui/comps/readme/wxsc.html#_2-时间格式转换方法" class="sidebar-link">2. 时间格式转换方法</a></li><li class="sidebar-sub-header"><a href="/mall-ui/comps/readme/wxsc.html#_3-用户信息储存到state的方法" class="sidebar-link">3. 用户信息储存到state的方法</a></li><li class="sidebar-sub-header"><a href="/mall-ui/comps/readme/wxsc.html#_4-获取有效值方法" class="sidebar-link">4. 获取有效值方法</a></li><li class="sidebar-sub-header"><a href="/mall-ui/comps/readme/wxsc.html#_5-不为空数组判断" class="sidebar-link">5. 不为空数组判断</a></li><li class="sidebar-sub-header"><a href="/mall-ui/comps/readme/wxsc.html#_6-判断元素是否为对象" class="sidebar-link">6. 判断元素是否为对象</a></li><li class="sidebar-sub-header"><a href="/mall-ui/comps/readme/wxsc.html#_7-获取时间对象方法" class="sidebar-link">7. 获取时间对象方法</a></li><li class="sidebar-sub-header"><a href="/mall-ui/comps/readme/wxsc.html#_8-微信小程序获取登录凭证code" class="sidebar-link">8. 微信小程序获取登录凭证code</a></li><li class="sidebar-sub-header"><a href="/mall-ui/comps/readme/wxsc.html#_9-默认用户名及头像" class="sidebar-link">9. 默认用户名及头像</a></li><li class="sidebar-sub-header"><a href="/mall-ui/comps/readme/wxsc.html#_10-判断session-key是否失效" class="sidebar-link">10. 判断session_key是否失效</a></li><li class="sidebar-sub-header"><a href="/mall-ui/comps/readme/wxsc.html#_11-获取系统信息" class="sidebar-link">11. 获取系统信息</a></li><li class="sidebar-sub-header"><a href="/mall-ui/comps/readme/wxsc.html#_12-验证手机号格式" class="sidebar-link">12. 验证手机号格式</a></li><li class="sidebar-sub-header"><a href="/mall-ui/comps/readme/wxsc.html#_13-设置h5标题栏文字" class="sidebar-link">13. 设置H5标题栏文字</a></li><li class="sidebar-sub-header"><a href="/mall-ui/comps/readme/wxsc.html#_14-验证内容是否含有表情" class="sidebar-link">14. 验证内容是否含有表情</a></li><li class="sidebar-sub-header"><a href="/mall-ui/comps/readme/wxsc.html#_15-保留两位小数" class="sidebar-link">15. 保留两位小数</a></li><li class="sidebar-sub-header"><a href="/mall-ui/comps/readme/wxsc.html#_16-session本地存储" class="sidebar-link">16. Session本地存储</a></li><li class="sidebar-sub-header"><a href="/mall-ui/comps/readme/wxsc.html#_17-获取当前页码" class="sidebar-link">17. 获取当前页码</a></li><li class="sidebar-sub-header"><a href="/mall-ui/comps/readme/wxsc.html#_18-样式对象转样式字符串" class="sidebar-link">18. 样式对象转样式字符串</a></li><li class="sidebar-sub-header"><a href="/mall-ui/comps/readme/wxsc.html#_19-storage本地缓存" class="sidebar-link">19. Storage本地缓存</a></li><li class="sidebar-sub-header"><a href="/mall-ui/comps/readme/wxsc.html#_20-延时-定时器" class="sidebar-link">20. 延时(定时器)</a></li></ul></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="商城共通方法"><a href="#商城共通方法" class="header-anchor">#</a> 商城共通方法</h1> <h2 id="_1-数组去重"><a href="#_1-数组去重" class="header-anchor">#</a> 1. 数组去重</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">noRepeat</span> <span class="token punctuation">(</span><span class="token parameter">arr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>    <span class="token comment">// 去重</span>
    <span class="token keyword">var</span> sameList <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> noReapeatList <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    arr<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item<span class="token punctuation">,</span>index</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>arr<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>item<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token operator">===</span> index<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">//当前元素，在原始数组中的第一个索引==当前索引值，否则返回当前元素</span>
            noReapeatList<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span>
        <span class="token punctuation">}</span> 
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
    noReapeatList<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token parameter">item</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
        <span class="token keyword">var</span> all <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">c</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> c <span class="token operator">==</span> item
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>all<span class="token punctuation">.</span>length<span class="token operator">&gt;</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            obj<span class="token punctuation">.</span>prodId <span class="token operator">=</span> item<span class="token punctuation">;</span>
            obj<span class="token punctuation">.</span>num <span class="token operator">=</span> all<span class="token punctuation">.</span>length
        <span class="token punctuation">}</span>
        sameList<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> <span class="token punctuation">{</span>noReapeatList<span class="token punctuation">,</span>sameList<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>使用方法：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> noRepeat <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'util'</span>
noRepeatAction <span class="token operator">=</span> <span class="token function">noRepeat</span><span class="token punctuation">(</span>actionIdList<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string">'noReapeatList'</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 将活动id去重</span>
</code></pre></div><h2 id="_2-时间格式转换方法"><a href="#_2-时间格式转换方法" class="header-anchor">#</a> 2. 时间格式转换方法</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">formatTime</span><span class="token punctuation">(</span>date<span class="token punctuation">,</span> formatStr <span class="token operator">=</span> <span class="token string">'/'</span><span class="token punctuation">,</span> hasjoin <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> year <span class="token operator">=</span> date<span class="token punctuation">.</span><span class="token function">getFullYear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> month <span class="token operator">=</span> date<span class="token punctuation">.</span><span class="token function">getMonth</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> day <span class="token operator">=</span> date<span class="token punctuation">.</span><span class="token function">getDate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> hour <span class="token operator">=</span> date<span class="token punctuation">.</span><span class="token function">getHours</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> minute <span class="token operator">=</span> date<span class="token punctuation">.</span><span class="token function">getMinutes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> second <span class="token operator">=</span> date<span class="token punctuation">.</span><span class="token function">getSeconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>hasjoin<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token punctuation">[</span>year<span class="token punctuation">,</span> month<span class="token punctuation">,</span> day<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>formatNumber<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span>formatStr<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">' '</span> <span class="token operator">+</span> <span class="token punctuation">[</span>hour<span class="token punctuation">,</span> minute<span class="token punctuation">,</span> second<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>formatNumber<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">':'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token punctuation">{</span>
            date<span class="token operator">:</span> <span class="token punctuation">[</span>year<span class="token punctuation">,</span> month<span class="token punctuation">,</span> day<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>formatNumber<span class="token punctuation">)</span><span class="token punctuation">,</span>
            time<span class="token operator">:</span> <span class="token punctuation">[</span>hour<span class="token punctuation">,</span> minute<span class="token punctuation">,</span> second<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>formatNumber<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">function</span> <span class="token function">formatNumber</span><span class="token punctuation">(</span><span class="token parameter">n</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    n <span class="token operator">=</span> n<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> n<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">?</span> n <span class="token operator">:</span> <span class="token string">'0'</span> <span class="token operator">+</span> n
<span class="token punctuation">}</span>
</code></pre></div><p>使用方法：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> formatTime <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'util'</span>
time <span class="token operator">=</span> <span class="token function">formatTime</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">'-'</span><span class="token punctuation">,</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token comment">// 共有3个参数  1.data：转换的时间戳（必传）  </span>
<span class="token comment">//            2.formatStr：日期之间的间隔符号，默认‘/’(可选)</span>
<span class="token comment">//            3.hasjoin：是否切割成数组，默认true（可选）</span>
</code></pre></div><h2 id="_3-用户信息储存到state的方法"><a href="#_3-用户信息储存到state的方法" class="header-anchor">#</a> 3. 用户信息储存到state的方法</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">async</span> <span class="token keyword">function</span> <span class="token function">mallMiniInitFun</span><span class="token punctuation">(</span><span class="token parameter">result</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 用户信息储存的方法</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>
        result<span class="token punctuation">.</span>returnCode <span class="token operator">==</span> <span class="token number">1</span> <span class="token operator">&amp;&amp;</span>
        result<span class="token punctuation">[</span><span class="token string">'data'</span><span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span>
        result<span class="token punctuation">[</span><span class="token string">'data'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'memberInfo'</span><span class="token punctuation">]</span>
    <span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> getData <span class="token operator">=</span> result<span class="token punctuation">[</span><span class="token string">'data'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'memberInfo'</span><span class="token punctuation">]</span>
        helper<span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">state</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            state<span class="token punctuation">.</span>mallMessage <span class="token operator">=</span> result<span class="token punctuation">[</span><span class="token string">'data'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'memberInfo'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
            state<span class="token punctuation">.</span>mallMember<span class="token operator">=</span>state<span class="token punctuation">.</span>mallMessage<span class="token punctuation">.</span>mallMember<span class="token operator">||</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
            state<span class="token punctuation">.</span>mallMember<span class="token punctuation">.</span>headImgUrl<span class="token operator">=</span>state<span class="token punctuation">.</span>mallMember<span class="token punctuation">.</span>headImgUrl<span class="token operator">||</span><span class="token string">'@decorat/avatarUrl.jpg'</span><span class="token punctuation">;</span>
            state<span class="token punctuation">.</span>mallMember<span class="token punctuation">.</span>nickName<span class="token operator">=</span>state<span class="token punctuation">.</span>mallMember<span class="token punctuation">.</span>nickName<span class="token operator">||</span><span class="token string">'神秘食客'</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>
                state<span class="token punctuation">.</span>mallMessage<span class="token punctuation">.</span><span class="token function">hasOwnProperty</span><span class="token punctuation">(</span><span class="token string">'wxMemberInfo'</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span>
                state<span class="token punctuation">.</span>mallMessage<span class="token punctuation">[</span><span class="token string">'wxMemberInfo'</span><span class="token punctuation">]</span>
            <span class="token punctuation">)</span> <span class="token punctuation">{</span>
                state<span class="token punctuation">.</span>mpId <span class="token operator">=</span> state<span class="token punctuation">.</span>mallMessage<span class="token punctuation">[</span><span class="token string">'wxMemberInfo'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'mpId'</span><span class="token punctuation">]</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>state<span class="token punctuation">.</span>mallMessage<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                state<span class="token punctuation">.</span>cardInfo <span class="token operator">=</span> state<span class="token punctuation">[</span><span class="token string">'mallMessage'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'cardInfo'</span><span class="token punctuation">]</span> <span class="token operator">||</span> <span class="token string">''</span>
                state<span class="token punctuation">.</span>grpInfo <span class="token operator">=</span> state<span class="token punctuation">[</span><span class="token string">'mallMessage'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'grpInfo'</span><span class="token punctuation">]</span> <span class="token operator">||</span> <span class="token string">''</span>
            <span class="token punctuation">}</span>
           
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>getData<span class="token punctuation">[</span><span class="token string">'mallInfo'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            helper<span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">state</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
                state<span class="token punctuation">.</span>mallInfo <span class="token operator">=</span> getData<span class="token punctuation">[</span><span class="token string">'mallInfo'</span><span class="token punctuation">]</span>
                state<span class="token punctuation">.</span>mallId <span class="token operator">=</span> getData<span class="token punctuation">[</span><span class="token string">'mallInfo'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'id'</span><span class="token punctuation">]</span>
                state<span class="token punctuation">.</span>gcId <span class="token operator">=</span> getData<span class="token punctuation">[</span><span class="token string">'mallInfo'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'gcId'</span><span class="token punctuation">]</span>
                state<span class="token punctuation">.</span>scoreShowName <span class="token operator">=</span> getData<span class="token punctuation">[</span><span class="token string">'mallInfo'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'scoreShowName'</span><span class="token punctuation">]</span>
                state<span class="token punctuation">.</span>decoration<span class="token punctuation">.</span>baseInfo <span class="token operator">=</span> getData<span class="token punctuation">[</span><span class="token string">'colorTheme'</span><span class="token punctuation">]</span> <span class="token comment">// 主题色</span>
                state<span class="token punctuation">.</span>crmType <span class="token operator">=</span> getData<span class="token punctuation">[</span><span class="token string">'grpInfo'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'crmType'</span><span class="token punctuation">]</span>
            <span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>getData<span class="token punctuation">[</span><span class="token string">'grpMember'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            helper<span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token string">'grpMember'</span><span class="token punctuation">,</span> getData<span class="token punctuation">[</span><span class="token string">'grpMember'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>getData<span class="token punctuation">[</span><span class="token string">'cardInfo'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            helper<span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token string">'cardInfo'</span><span class="token punctuation">,</span> getData<span class="token punctuation">[</span><span class="token string">'cardInfo'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>getData<span class="token punctuation">[</span><span class="token string">'mallMember'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            helper<span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token string">'mallMemberId'</span><span class="token punctuation">,</span> getData<span class="token punctuation">[</span><span class="token string">'mallMember'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>使用方法：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> mallMiniInitFun <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'util'</span>
<span class="token function">mallMiniInitFun</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span>  <span class="token comment">// data是init接口请求到的用户数据</span>
</code></pre></div><h2 id="_4-获取有效值方法"><a href="#_4-获取有效值方法" class="header-anchor">#</a> 4. 获取有效值方法</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">isValidValue</span><span class="token punctuation">(</span><span class="token parameter">father<span class="token punctuation">,</span> key<span class="token punctuation">,</span> defaultValue</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> nowValue <span class="token operator">=</span> father <span class="token operator">?</span> father<span class="token punctuation">[</span>key<span class="token punctuation">]</span> <span class="token operator">:</span> father <span class="token comment">// 当前值</span>
    <span class="token keyword">let</span> checkResult <span class="token operator">=</span> <span class="token boolean">true</span> <span class="token comment">// 检测结果</span>
    <span class="token keyword">let</span> hasSetDefaultValue <span class="token operator">=</span> defaultValue <span class="token operator">!==</span> <span class="token keyword">undefined</span> <span class="token operator">||</span> defaultValue <span class="token operator">!==</span> <span class="token keyword">null</span>
    <span class="token comment">// 剩下的就是数字或者字符串以及布尔值，直接返回</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>nowValue<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        checkResult <span class="token operator">=</span> nowValue <span class="token operator">&amp;&amp;</span> nowValue<span class="token punctuation">.</span>length <span class="token comment">// 数组内是否有有效值的检测</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>hasSetDefaultValue<span class="token punctuation">)</span> defaultValue <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">isObject2</span><span class="token punctuation">(</span>nowValue<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> keys <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span>nowValue<span class="token punctuation">)</span>
        checkResult <span class="token operator">=</span> nowValue <span class="token operator">&amp;&amp;</span> keys <span class="token operator">&amp;&amp;</span> keys<span class="token punctuation">.</span>length <span class="token comment">// 对象内是否有键值</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>hasSetDefaultValue<span class="token punctuation">)</span> defaultValue <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>checkResult<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token parameter">nextKey<span class="token punctuation">,</span> newDefaultValue</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>
                nextKey <span class="token operator">||</span>
                nextKey <span class="token operator">===</span> <span class="token number">0</span> <span class="token operator">||</span>
                nextKey <span class="token operator">===</span> <span class="token boolean">false</span> <span class="token operator">||</span>
                nextKey <span class="token operator">===</span> <span class="token string">''</span>
            <span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token comment">// 如果准备往下找一下层</span>
                <span class="token keyword">return</span> <span class="token function">isValidValue</span><span class="token punctuation">(</span>nowValue<span class="token punctuation">,</span> nextKey<span class="token punctuation">,</span> newDefaultValue<span class="token punctuation">)</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>nowValue <span class="token operator">===</span> <span class="token keyword">undefined</span> <span class="token operator">||</span> nowValue <span class="token operator">===</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token comment">// 如果最终的值无效</span>
                <span class="token keyword">return</span> defaultValue
            <span class="token punctuation">}</span>
            <span class="token comment">// 如果最终的值有效</span>
            <span class="token keyword">return</span> nowValue
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> defaultValue
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>使用方法：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * 获取有效值，返回值或默认值
 * @param {*} father 父级
 * @param {*} key    键值
 * @param {*} defaultValue 自定义默认值
 */</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> isValidValue <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'util'</span>
styleRes <span class="token operator">=</span> <span class="token function">isValidValue</span><span class="token punctuation">(</span>result<span class="token punctuation">,</span> <span class="token string">'styles'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 
</code></pre></div><h2 id="_5-不为空数组判断"><a href="#_5-不为空数组判断" class="header-anchor">#</a> 5. 不为空数组判断</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">isValidArray</span><span class="token punctuation">(</span><span class="token parameter">arr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">Boolean</span><span class="token punctuation">(</span>Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> arr <span class="token operator">&amp;&amp;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><p>使用方法：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> isValidArray <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'util'</span>
<span class="token function">isValidArray</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span>  
<span class="token comment">// arr是不为空数组返回 true ，否则返回 false</span>
</code></pre></div><h2 id="_6-判断元素是否为对象"><a href="#_6-判断元素是否为对象" class="header-anchor">#</a> 6. 判断元素是否为对象</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">isType</span><span class="token punctuation">(</span><span class="token parameter">obj<span class="token punctuation">,</span> typeString</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token operator">===</span> typeString
<span class="token punctuation">}</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">isObject2</span><span class="token punctuation">(</span><span class="token parameter">obj</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">isType</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token string">'[object Object]'</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><p>使用方法：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> isObject2 <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'util'</span>
<span class="token function">isObject2</span><span class="token punctuation">(</span>object<span class="token punctuation">)</span>  
<span class="token comment">// object是对象类型返回 true ，否则返回 false</span>
</code></pre></div><h2 id="_7-获取时间对象方法"><a href="#_7-获取时间对象方法" class="header-anchor">#</a> 7. 获取时间对象方法</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">timerObj</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>value<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        obj <span class="token operator">=</span> <span class="token punctuation">{</span> dayNum<span class="token operator">:</span> <span class="token string">'00'</span><span class="token punctuation">,</span> hourNum<span class="token operator">:</span> <span class="token string">'00'</span><span class="token punctuation">,</span> minNum<span class="token operator">:</span> <span class="token string">'00'</span><span class="token punctuation">,</span> secNum<span class="token operator">:</span> <span class="token string">'00'</span> <span class="token punctuation">}</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> endTime <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">var</span> now <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">var</span> reduce <span class="token operator">=</span> endTime <span class="token operator">-</span> now
        <span class="token keyword">var</span> day <span class="token operator">=</span> <span class="token number">24</span> <span class="token operator">*</span> <span class="token number">3600</span> <span class="token operator">*</span> <span class="token number">1000</span>
        <span class="token keyword">var</span> hour <span class="token operator">=</span> <span class="token number">3600</span> <span class="token operator">*</span> <span class="token number">1000</span>
        <span class="token keyword">var</span> minute <span class="token operator">=</span> <span class="token number">60</span> <span class="token operator">*</span> <span class="token number">1000</span>
        <span class="token keyword">var</span> dayNum <span class="token operator">=</span> Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span>reduce <span class="token operator">/</span> day<span class="token punctuation">)</span>
        <span class="token keyword">var</span> hourNum <span class="token operator">=</span> Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span><span class="token punctuation">(</span>reduce <span class="token operator">-</span> dayNum <span class="token operator">*</span> day<span class="token punctuation">)</span> <span class="token operator">/</span> hour<span class="token punctuation">)</span>
        <span class="token keyword">var</span> minNum <span class="token operator">=</span> Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span>
            <span class="token punctuation">(</span>reduce <span class="token operator">-</span> dayNum <span class="token operator">*</span> day <span class="token operator">-</span> hourNum <span class="token operator">*</span> hour<span class="token punctuation">)</span> <span class="token operator">/</span> minute
        <span class="token punctuation">)</span>
        <span class="token keyword">var</span> secNum <span class="token operator">=</span> Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span>
            <span class="token punctuation">(</span>reduce <span class="token operator">-</span> dayNum <span class="token operator">*</span> day <span class="token operator">-</span> hourNum <span class="token operator">*</span> hour <span class="token operator">-</span> minNum <span class="token operator">*</span> minute<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">1000</span>
        <span class="token punctuation">)</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>dayNum <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            dayNum <span class="token operator">=</span> <span class="token string">'0'</span> <span class="token operator">+</span> dayNum
        <span class="token punctuation">}</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>hourNum <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            hourNum <span class="token operator">=</span> <span class="token string">'0'</span> <span class="token operator">+</span> hourNum
        <span class="token punctuation">}</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>minNum <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            minNum <span class="token operator">=</span> <span class="token string">'0'</span> <span class="token operator">+</span> minNum
        <span class="token punctuation">}</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>secNum <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            secNum <span class="token operator">=</span> <span class="token string">'0'</span> <span class="token operator">+</span> secNum
        <span class="token punctuation">}</span>
        obj <span class="token operator">=</span> <span class="token punctuation">{</span> dayNum<span class="token punctuation">,</span> hourNum<span class="token punctuation">,</span> minNum<span class="token punctuation">,</span> secNum <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> obj
<span class="token punctuation">}</span>
</code></pre></div><p>使用方法：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> timerObj <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'util'</span>
<span class="token function">timerObj</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>  

<span class="token comment">// 有1个可选参数 传入date类型 例如：value = &quot;2021/09/30 00:00:00&quot;</span>
<span class="token comment">// 有参数 ： 返回 传入日期-当前日期 剩余 天时分秒</span>
<span class="token comment">// 无参数 ： 返回  { dayNum: '00', hourNum: '00', minNum: '00', secNum: '00' }</span>
</code></pre></div><h2 id="_8-微信小程序获取登录凭证code"><a href="#_8-微信小程序获取登录凭证code" class="header-anchor">#</a> 8. 微信小程序获取登录凭证code</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">login</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">resolve<span class="token punctuation">,</span> reject</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        uni<span class="token punctuation">.</span><span class="token function">login</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
            provider<span class="token operator">:</span> <span class="token string">'weixin'</span><span class="token punctuation">,</span>
            <span class="token function-variable function">success</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span>res<span class="token punctuation">.</span>code<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token function">resolve</span><span class="token punctuation">(</span>res<span class="token punctuation">.</span>code<span class="token punctuation">)</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token function-variable function">fail</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token function">reject</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><p>使用方法：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> login <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'util'</span>
<span class="token function">login</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 
</code></pre></div><h2 id="_9-默认用户名及头像"><a href="#_9-默认用户名及头像" class="header-anchor">#</a> 9. 默认用户名及头像</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">let</span> defaultNickName <span class="token operator">=</span> <span class="token string">'神秘食客'</span>
<span class="token keyword">export</span> <span class="token keyword">let</span> defaultAvatarUrl <span class="token operator">=</span><span class="token string">'@decorat/avatarUrl.jpg'</span>
</code></pre></div><p>使用方法：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> defaultNickName<span class="token punctuation">,</span>defaultAvatarUrl <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'util'</span>
<span class="token function">data</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  nickName<span class="token operator">:</span>defaultNickName<span class="token punctuation">,</span>
  avatarUrl<span class="token operator">:</span>defaultAvatarUrl<span class="token punctuation">,</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="_10-判断session-key是否失效"><a href="#_10-判断session-key是否失效" class="header-anchor">#</a> 10. 判断session_key是否失效</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">checkSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">resolve<span class="token punctuation">,</span> reject</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        uni<span class="token punctuation">.</span><span class="token function">checkSession</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
            <span class="token function-variable function">success</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
                console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'session_key未失效'</span><span class="token punctuation">)</span>
                <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token function-variable function">fail</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
                console<span class="token punctuation">.</span><span class="token function">warn</span><span class="token punctuation">(</span><span class="token string">'session_key失效，重新login'</span><span class="token punctuation">)</span>
                <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><p>使用方法：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> checkSession <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'util'</span>
<span class="token function">checkSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">// 返回值布尔类型：  false -- 未失效   true -- 已失效</span>
</code></pre></div><h2 id="_11-获取系统信息"><a href="#_11-获取系统信息" class="header-anchor">#</a> 11. 获取系统信息</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">let</span> uniSystemInfo <span class="token operator">=</span> <span class="token keyword">null</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">getSystemInfoSync</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>uniSystemInfo<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        uniSystemInfo <span class="token operator">=</span> uni<span class="token punctuation">.</span><span class="token function">getSystemInfoSync</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> uniSystemInfo
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> uniSystemInfo
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">getClientHeight</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">try</span> <span class="token punctuation">{</span>
        <span class="token keyword">const</span> res <span class="token operator">=</span> <span class="token function">getSystemInfoSync</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> res<span class="token punctuation">.</span>windowHeight
    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// error</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">getClientWidth</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">try</span> <span class="token punctuation">{</span>
        <span class="token keyword">const</span> res <span class="token operator">=</span> <span class="token function">getSystemInfoSync</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> res<span class="token punctuation">.</span>windowWidth
    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// error</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">getStatusBarHeight</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">try</span> <span class="token punctuation">{</span>
        <span class="token keyword">const</span> res <span class="token operator">=</span> <span class="token function">getSystemInfoSync</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> res<span class="token punctuation">.</span>statusBarHeight
    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// error</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>使用方法：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> getSystemInfoSync<span class="token punctuation">,</span>getClientHeight<span class="token punctuation">,</span>getClientWidth<span class="token punctuation">,</span>getStatusBarHeight <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'util'</span>
<span class="token function">getSystemInfoSync</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment">// 获取系统信息</span>
<span class="token function">getClientHeight</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment">// 获取可使用窗口高度</span>
<span class="token function">getClientWidth</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment">// 获取可使用窗口宽度</span>
<span class="token function">getStatusBarHeight</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment">// 获取状态栏的高度	</span>
</code></pre></div><h2 id="_12-验证手机号格式"><a href="#_12-验证手机号格式" class="header-anchor">#</a> 12. 验证手机号格式</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">checkPhone</span><span class="token punctuation">(</span><span class="token parameter">phone</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> mobilePatrn <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^(13[0-9]|14[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[0-9])\d{8}$</span><span class="token regex-delimiter">/</span></span>
    <span class="token keyword">return</span> mobilePatrn<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>phone<span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token boolean">true</span> <span class="token operator">:</span> <span class="token boolean">false</span>
<span class="token punctuation">}</span>
</code></pre></div><p>使用方法：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> checkPhone <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'util'</span>
<span class="token function">checkPhone</span><span class="token punctuation">(</span>phone<span class="token punctuation">)</span> <span class="token comment">// 返回值布尔类型：  false -- 验证失败   true -- 验证通过</span>
</code></pre></div><h2 id="_13-设置h5标题栏文字"><a href="#_13-设置h5标题栏文字" class="header-anchor">#</a> 13. 设置H5标题栏文字</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">setWechatTitle</span><span class="token punctuation">(</span><span class="token parameter">title</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>title <span class="token operator">===</span> <span class="token keyword">undefined</span> <span class="token operator">||</span> window<span class="token punctuation">.</span>document<span class="token punctuation">.</span>title <span class="token operator">===</span> title<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span>
    <span class="token punctuation">}</span>
    document<span class="token punctuation">.</span>title <span class="token operator">=</span> title
    <span class="token keyword">var</span> mobile <span class="token operator">=</span> navigator<span class="token punctuation">.</span>userAgent<span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">iphone|ipad|ipod</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>mobile<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> iframe <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">createElement</span><span class="token punctuation">(</span><span class="token string">'iframe'</span><span class="token punctuation">)</span>
        iframe<span class="token punctuation">.</span>style<span class="token punctuation">.</span>display <span class="token operator">=</span> <span class="token string">'none'</span>
        <span class="token keyword">var</span> <span class="token function-variable function">iframeCallback</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                iframe<span class="token punctuation">.</span><span class="token function">removeEventListener</span><span class="token punctuation">(</span><span class="token string">'load'</span><span class="token punctuation">,</span> iframeCallback<span class="token punctuation">)</span>
                document<span class="token punctuation">.</span>body<span class="token punctuation">.</span><span class="token function">removeChild</span><span class="token punctuation">(</span>iframe<span class="token punctuation">)</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
        iframe<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">'load'</span><span class="token punctuation">,</span> iframeCallback<span class="token punctuation">)</span>
        document<span class="token punctuation">.</span>body<span class="token punctuation">.</span><span class="token function">appendChild</span><span class="token punctuation">(</span>iframe<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>使用方法：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> setWechatTitle <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'util'</span>
<span class="token function">setWechatTitle</span><span class="token punctuation">(</span>title<span class="token punctuation">)</span> 
</code></pre></div><h2 id="_14-验证内容是否含有表情"><a href="#_14-验证内容是否含有表情" class="header-anchor">#</a> 14. 验证内容是否含有表情</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">isEmojiCharacter</span><span class="token punctuation">(</span><span class="token parameter">substring</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> substring<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> hs <span class="token operator">=</span> substring<span class="token punctuation">.</span><span class="token function">charCodeAt</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>hs <span class="token operator">&gt;=</span> <span class="token number">0xd800</span> <span class="token operator">&amp;&amp;</span> hs <span class="token operator">&lt;=</span> <span class="token number">0xdbff</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>substring<span class="token punctuation">.</span>length <span class="token operator">&gt;</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">var</span> ls <span class="token operator">=</span> substring<span class="token punctuation">.</span><span class="token function">charCodeAt</span><span class="token punctuation">(</span>i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span>
                <span class="token keyword">var</span> uc <span class="token operator">=</span> <span class="token punctuation">(</span>hs <span class="token operator">-</span> <span class="token number">0xd800</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">0x400</span> <span class="token operator">+</span> <span class="token punctuation">(</span>ls <span class="token operator">-</span> <span class="token number">0xdc00</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">0x10000</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span>uc <span class="token operator">&gt;=</span> <span class="token number">0x1d000</span> <span class="token operator">&amp;&amp;</span> uc <span class="token operator">&lt;=</span> <span class="token number">0x1f77f</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token keyword">return</span> <span class="token boolean">true</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>substring<span class="token punctuation">.</span>length <span class="token operator">&gt;</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">var</span> _ls <span class="token operator">=</span> substring<span class="token punctuation">.</span><span class="token function">charCodeAt</span><span class="token punctuation">(</span>i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>_ls <span class="token operator">==</span> <span class="token number">0x20e3</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token boolean">true</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>hs <span class="token operator">&gt;=</span> <span class="token number">0x2100</span> <span class="token operator">&amp;&amp;</span> hs <span class="token operator">&lt;=</span> <span class="token number">0x27ff</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token boolean">true</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>hs <span class="token operator">&gt;=</span> <span class="token number">0x2b05</span> <span class="token operator">&amp;&amp;</span> hs <span class="token operator">&lt;=</span> <span class="token number">0x2b07</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token boolean">true</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>hs <span class="token operator">&gt;=</span> <span class="token number">0x2934</span> <span class="token operator">&amp;&amp;</span> hs <span class="token operator">&lt;=</span> <span class="token number">0x2935</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token boolean">true</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>hs <span class="token operator">&gt;=</span> <span class="token number">0x3297</span> <span class="token operator">&amp;&amp;</span> hs <span class="token operator">&lt;=</span> <span class="token number">0x3299</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token boolean">true</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>
                hs <span class="token operator">==</span> <span class="token number">0xa9</span> <span class="token operator">||</span>
                hs <span class="token operator">==</span> <span class="token number">0xae</span> <span class="token operator">||</span>
                hs <span class="token operator">==</span> <span class="token number">0x303d</span> <span class="token operator">||</span>
                hs <span class="token operator">==</span> <span class="token number">0x3030</span> <span class="token operator">||</span>
                hs <span class="token operator">==</span> <span class="token number">0x2b55</span> <span class="token operator">||</span>
                hs <span class="token operator">==</span> <span class="token number">0x2b1c</span> <span class="token operator">||</span>
                hs <span class="token operator">==</span> <span class="token number">0x2b1b</span> <span class="token operator">||</span>
                hs <span class="token operator">==</span> <span class="token number">0x2b50</span>
            <span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token boolean">true</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>使用方法：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> isEmojiCharacter <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'util'</span>
<span class="token function">isEmojiCharacter</span><span class="token punctuation">(</span>text<span class="token punctuation">)</span>  <span class="token comment">// 返回值布尔类型：  false -- 内容未含有表情   true -- 内容含有表情</span>
</code></pre></div><h2 id="_15-保留两位小数"><a href="#_15-保留两位小数" class="header-anchor">#</a> 15. 保留两位小数</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">Fixed2</span><span class="token punctuation">(</span><span class="token parameter">num</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> newNum <span class="token operator">=</span> num <span class="token operator">+</span> <span class="token string">''</span>
    <span class="token keyword">var</span> isSplit <span class="token operator">=</span> <span class="token boolean">true</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>newNum<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\.</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> strArr <span class="token operator">=</span> newNum<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">'.'</span><span class="token punctuation">)</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>strArr<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>length <span class="token operator">&gt;</span> <span class="token number">8</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            isSplit <span class="token operator">=</span> <span class="token boolean">false</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">var</span> aNew
    <span class="token keyword">var</span> re <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">([0-9]+\.[0-9]{2})[0-9]*</span><span class="token regex-delimiter">/</span></span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>isSplit<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        aNew <span class="token operator">=</span> newNum<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>re<span class="token punctuation">,</span> <span class="token string">'$1'</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        aNew <span class="token operator">=</span> <span class="token function">Number</span><span class="token punctuation">(</span>newNum<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toFixed</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token function">Number</span><span class="token punctuation">(</span>aNew<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><p>使用方法：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> Fixed2 <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'util'</span>
<span class="token function">Fixed2</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span>  <span class="token comment">// 返回保留两位小数</span>
</code></pre></div><h2 id="_16-session本地存储"><a href="#_16-session本地存储" class="header-anchor">#</a> 16. Session本地存储</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">getSessionData</span><span class="token punctuation">(</span><span class="token parameter">key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> value <span class="token operator">=</span> <span class="token string">''</span>
    <span class="token comment">//#ifdef H5</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>window<span class="token punctuation">.</span>sessionStorage<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        value <span class="token operator">=</span> window<span class="token punctuation">.</span>sessionStorage<span class="token punctuation">.</span><span class="token function">getItem</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    <span class="token comment">//#endif</span>
    <span class="token comment">//#ifdef MP</span>
    value <span class="token operator">=</span> helper<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>
    <span class="token comment">//#endif</span>
    <span class="token keyword">return</span> value
<span class="token punctuation">}</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">clearSessionData</span><span class="token punctuation">(</span><span class="token parameter">key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">//#ifdef H5</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>window<span class="token punctuation">.</span>sessionStorage<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>key<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            window<span class="token punctuation">.</span>sessionStorage<span class="token punctuation">.</span><span class="token function">removeItem</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            window<span class="token punctuation">.</span>sessionStorage<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token comment">//#endif</span>
    <span class="token comment">//#ifdef MP</span>
    helper<span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span>
    <span class="token comment">//#endif</span>
<span class="token punctuation">}</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">storeSessionData</span><span class="token punctuation">(</span><span class="token parameter">key<span class="token punctuation">,</span> value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">//#ifdef H5</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>window<span class="token punctuation">.</span>sessionStorage<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        window<span class="token punctuation">.</span>sessionStorage<span class="token punctuation">.</span><span class="token function">setItem</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> value<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    <span class="token comment">//#endif</span>
    <span class="token comment">//#ifdef MP</span>
    helper<span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> value<span class="token punctuation">)</span>
    <span class="token comment">//#endif</span>
<span class="token punctuation">}</span>
</code></pre></div><p>使用方法：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> getSessionData<span class="token punctuation">,</span>clearSessionData<span class="token punctuation">,</span>storeSessionData <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'util'</span>
<span class="token function">getSessionData</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>  <span class="token comment">// 读取本地数据</span>
<span class="token function">clearSessionData</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>  <span class="token comment">// 清除本地数据</span>
<span class="token function">storeSessionData</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>  <span class="token comment">// 设置本地数据</span>
</code></pre></div><h2 id="_17-获取当前页码"><a href="#_17-获取当前页码" class="header-anchor">#</a> 17. 获取当前页码</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">getCurrentPage</span><span class="token punctuation">(</span><span class="token parameter">nowGet<span class="token operator">=</span><span class="token boolean">false</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> currentPageValue <span class="token operator">=</span> helper<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token string">'currentPage'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> key <span class="token operator">=</span> <span class="token string">''</span>
    <span class="token keyword">var</span> page <span class="token operator">=</span> <span class="token string">''</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>currentPageValue <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span>nowGet<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        key <span class="token operator">=</span> currentPageValue<span class="token punctuation">;</span>
        page <span class="token operator">=</span> pageConfig<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> pages <span class="token operator">=</span> <span class="token function">getCurrentPages</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">var</span> page <span class="token operator">=</span> pages<span class="token punctuation">[</span>pages<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span>
        <span class="token keyword">var</span> path <span class="token operator">=</span> page<span class="token punctuation">.</span>route<span class="token punctuation">;</span>
        <span class="token keyword">var</span> keyList <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span>pageConfig<span class="token punctuation">)</span>
        keyList<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>pageConfig<span class="token punctuation">[</span>item<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'path'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                key <span class="token operator">=</span> item
                <span class="token keyword">return</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
        page <span class="token operator">=</span> pageConfig<span class="token punctuation">[</span>key<span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>key<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token punctuation">{</span> key<span class="token punctuation">,</span> page <span class="token punctuation">}</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> key
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>使用方法：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> getCurrentPage <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'util'</span>
<span class="token function">getCurrentPage</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  
</code></pre></div><h2 id="_18-样式对象转样式字符串"><a href="#_18-样式对象转样式字符串" class="header-anchor">#</a> 18. 样式对象转样式字符串</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">transformCssText</span><span class="token punctuation">(</span><span class="token parameter">obj</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// style object ===&gt; string</span>
    <span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">''</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token string">'[object Object]'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> keys <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>keys<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> str
        <span class="token punctuation">}</span>
        keys<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            str <span class="token operator">+=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>item<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">:</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>obj<span class="token punctuation">[</span>item<span class="token punctuation">]</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">;</span><span class="token template-punctuation string">`</span></span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> str
<span class="token punctuation">}</span>
</code></pre></div><p>使用方法：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> transformCssText <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'util'</span>
<span class="token function">transformCssText</span><span class="token punctuation">(</span>styleObj<span class="token punctuation">)</span>  
<span class="token comment">// 例如：styleObj = {color:&quot;#fff&quot;,background:&quot;#000&quot;}</span>
<span class="token comment">// 返回 'color:#fff;background:#000;'</span>
</code></pre></div><h2 id="_19-storage本地缓存"><a href="#_19-storage本地缓存" class="header-anchor">#</a> 19. Storage本地缓存</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">getStorageSync</span><span class="token punctuation">(</span><span class="token parameter">key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">//2020.1.6 微信官方说 有些手机不一定每次都能取到值  官网的demo都有try..catch</span>
    <span class="token keyword">var</span> value <span class="token operator">=</span> <span class="token string">''</span>
    <span class="token keyword">try</span> <span class="token punctuation">{</span>
        value <span class="token operator">=</span> uni<span class="token punctuation">.</span><span class="token function">getStorageSync</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>
    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// Do something when catch error</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> value
<span class="token punctuation">}</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">removeStorageSync</span><span class="token punctuation">(</span><span class="token parameter">key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">try</span> <span class="token punctuation">{</span>
        uni<span class="token punctuation">.</span><span class="token function">removeStorageSync</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>
    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// Do something when catch error</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">clearStorageSync</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">try</span> <span class="token punctuation">{</span>
        uni<span class="token punctuation">.</span><span class="token function">clearStorageSync</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// error</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">setStorageSync</span><span class="token punctuation">(</span><span class="token parameter">key<span class="token punctuation">,</span> value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">try</span> <span class="token punctuation">{</span>
        uni<span class="token punctuation">.</span><span class="token function">setStorageSync</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> value<span class="token punctuation">)</span>
    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// error</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>使用方法：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> getStorageSync<span class="token punctuation">,</span>removeStorageSync<span class="token punctuation">,</span>clearStorageSync<span class="token punctuation">,</span>setStorageSync <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'util'</span>
<span class="token function">getStorageSync</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>  <span class="token comment">// 从本地缓存中同步获取指定 key 对应的内容。</span>
<span class="token function">removeStorageSync</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>  <span class="token comment">// 从本地缓存中同步移除指定 key。</span>
<span class="token function">clearStorageSync</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>  <span class="token comment">// 同步清理本地数据缓存。</span>
<span class="token function">setStorageSync</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>  <span class="token comment">// 将 data 存储在本地缓存中指定的 key 中，会覆盖掉原来该 key 对应的内容</span>
</code></pre></div><h2 id="_20-延时-定时器"><a href="#_20-延时-定时器" class="header-anchor">#</a> 20. 延时(定时器)</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">delay</span><span class="token punctuation">(</span><span class="token parameter">timer <span class="token operator">=</span> <span class="token number">200</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">resolve<span class="token punctuation">,</span> reject</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span> timer<span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><p>使用方法：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> delay <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'util'</span>
<span class="token function">delay</span><span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">)</span>  <span class="token comment">// 传入number类型，默认200毫秒</span>
</code></pre></div></div> <footer class="page-edit"><!----> <!----></footer> <!----> </main></div><div class="global-ui"><!----></div></div>
    <script src="/mall-ui/assets/js/app.71961e7f.js" defer></script><script src="/mall-ui/assets/js/2.f1cb9681.js" defer></script><script src="/mall-ui/assets/js/13.06b07c8e.js" defer></script>
  </body>
</html>
